
/*Copyright (c)  Sergey Schekochikhin (Genocidicbunny)
 * created on Sep 30, 2009 10:55:56 PM
 */
package Mash.Map;

import Mash.Utility.TerrainMapRead;
import Mash.Utility.Location;

import java.io.IOException;
import java.io.Serializable;

public class TerrainMap implements Serializable{
	private TerrainType[][] terraMap;
	//Creates a basic, empty terrain map. Not sure of any uses for this, maybe Ill come up with one later.
	public TerrainMap(int row, int col){
		terraMap = new TerrainType[row][col];
	}
	//If we want the map to be generated instead of loaded we use this constructor. Boolean generate designates what
	// kind of generation we want. True covers the entire map in grassland. False will eventually create a generated map.
	//This constructor will only be used for a future scenario editor. Otherwise, read onto next constructor
//	public TerrainMap(int row, int col, boolean generate){
		 //implementing a map generator is possible, but quite unlikely. For now, this construction is a placeholder
//	}
	//Reads in a terrain map from a .tmap file, which is essentially a text file in the following format:
	/*
	 *>rows on map [as a single int only, no other characters permitted]
	 *>cols on map [also as a single int only, no other characters permitted]
	 *>Comma separated list of terrain types, such as: 0,1,2,3,4,5,6 There must be as many ints as there are cols
	 *>repeat above for however many rows were defined
	 * An example file for a 4x4 map:
	 * 4
	 * 4
	 * 0,1,2,3
	 * 4,5,6,0
	 * 1,2,3,4
	 * 5,6,0,1
	 * See TerrainMapRead.chooseTerrain for further info on what terrain type is what.
	 */
	public TerrainMap(String fileLoc){
		try{
			terraMap = TerrainMapRead.readInTerrainMap(fileLoc);
		}catch( IOException ioe){System.out.println("File not found!!"); ioe.printStackTrace();}
	}

	///Accessors/Modifiers
	//Returns TerrainType at the defined location
	public TerrainType getTType(int row, int col){
		return terraMap[row][col];
	}
	//Sets the defined location to the TerrainType passed to the method
	public void setTType(int row, int col, TerrainType tt){
		terraMap[row][col] = tt;
	}
	//Returns an Array of Cardinal Locations to the supplied coordinate. If a location is not valid, the value for that
	//direction will be null
	//Coordinates are returned in the following order: NORTH, EAST, SOUTH, WEST
	public Location[] getNeighborLocs(int row, int col){
		Location[] neighbors = new Location[4];
		if(isValidLoc(row-1, col))    //NORTH
			neighbors[0] = new Location(row-1, col);
		else
			neighbors[0] = null;

		if(isValidLoc(row, col+1))     //EAST
			neighbors[1] = new Location(row, col+1);
		else
			neighbors[1] = null;

		if(isValidLoc(row+1, col))      //SOUTH
			neighbors[2] = new Location(row+1, col);
		else
			neighbors[2] = null;

		if(isValidLoc(row, col-1))      //WEST
		    neighbors[3] = new Location(row, col-1);
		else
			neighbors[3] = null;

		return neighbors;

	}
	//helper method to check whether or not a given location is valid.
	private boolean isValidLoc(int row, int col){
		try{
			TerrainType t = terraMap[row][col];
			return true;
		}catch(ArrayIndexOutOfBoundsException aiofb){return false;}
	}
	//Debugging method -- will print out a text-based version of the currently stored map
	public void printCurrentMap(){
		for(int i = 0; i < terraMap.length; i++){
			for(int j = 0; j < terraMap[0].length; j++){
				if(terraMap[i][j] == TerrainType.GRASSLAND)
					System.out.print("G ");
				else if(terraMap[i][j] == TerrainType.ROAD)
					System.out.print("R ");
				else if(terraMap[i][j] == TerrainType.BRIDGE)
					System.out.print("B ");
				else if(terraMap[i][j] == TerrainType.COAST_SHALLOWS)
					System.out.print("C ");
				else if(terraMap[i][j] == TerrainType.FOREST)
					System.out.print("F ");
				else if(terraMap[i][j] == TerrainType.MOUNTAIN)
					System.out.print("M ");
				else if(terraMap[i][j] == TerrainType.OCEAN)
					System.out.print("O ");
				else
					System.out.print("!");
			}
			System.out.println();
		}
	}
	//Debugging only. Should never be used/called much less exist in the real thing
	public static void main(String args[]){
		TerrainMap tmap = new TerrainMap("\\Mash\\Map\\test.tmap");
		System.out.println("Got the map fine");
		tmap.printCurrentMap();
		System.out.println(tmap.isValidLoc(1,2));
	}
}
